var slide = function(option) {
    var defaults = {
        container: '',
        loading: '',
        next: function() {}
    }
    var start,
        end,
        length,
        isLock = false, //是否锁定整个操作
        isCanDo = false, //是否移动滑块
        isTouchPad = (/hp-tablet/gi).test(navigator.appVersion),
        hasTouch = 'ontouchstart' in window && !isTouchPad;
    var obj = document.querySelector(option.container);
    var loading = document.querySelector(option.loading);
    var offset= 0;
    var hold = option.hold || 50;
    var objparent = obj.parentElement;
    /*操作方法*/
    var fn = {
        //移动容器
        translate: function(diff) {
            obj.style.webkitTransform = 'translate3d(0,' + diff + 'px,0)';
            obj.style.transform = 'translate3d(0,' + diff + 'px,0)';
        },
        //设置效果时间
        setTransition: function(time) {
            obj.style.webkitTransition = 'all ' + time + 's';
            obj.style.transition = 'all ' + time + 's';
        },
        //返回到初始位置
        back: function() {
            fn.translate(0 - offset);
            //标识操作完成
            loading.style.display = 'none';
            isLock = false;
        },
        addEvent: function(element, event_name, event_fn) {
            if (element.addEventListener) {
                element.addEventListener(event_name, event_fn, false);
            } else if (element.attachEvent) {
                element.attachEvent('on' + event_name, event_fn);
            } else {
                element['on' + event_name] = event_fn;
            }
        }
    };

    fn.translate(0 - offset);
    fn.addEvent(obj, 'touchstart', start);
    fn.addEvent(obj, 'touchmove', move);
    fn.addEvent(obj, 'touchend', end);
    fn.addEvent(obj, 'mousedown', start)
    fn.addEvent(obj, 'mousemove', move)
    fn.addEvent(obj, 'mouseup', end)

    //滑动开始
    function start(e) {
        var top = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
        if (top <= 0 && !isLock) {
            var even = typeof event == "undefined" ? e : event;
            if (!event.touches) {return false;}
            //标识操作进行中
            isLock = true;
            isCanDo = true;
            //保存当前鼠标Y坐标
            start = hasTouch ? even.touches[0].pageY : even.pageY;
            //消除滑块动画时间
            fn.setTransition(0);
        }
        return false;
    }

    //滑动中
    function move(e) {
        if (objparent.scrollTop <= 0 && isCanDo) {
            var even = typeof event == "undefined" ? e : event;
            //保存当前鼠标Y坐标
            end = hasTouch ? even.touches[0].pageY : even.pageY;
            if (start < end) {
                even.preventDefault();
                //消除滑块动画时间
                fn.setTransition(0);
                //移动滑块
                if ((end - start - offset) / 2 <= 150) {
                    length = (end - start - offset) / 2;
                    fn.translate(length);
                } else {
                    length += 0.3;
                    fn.translate(length);
                }
            }
        }
    }
    //滑动结束
    function end(e) {
        if (isCanDo) {
            isCanDo = false;
            //判断滑动距离是否大于等于指定值
            if (end - start >= hold) {
                //设置滑块回弹时间
                fn.setTransition(1);
                //保留提示部分
                fn.translate(0);
                //执行回调函数
                loading.style.display = 'block';
                if (typeof option.next == "function") {
                    option.next.call(fn, e);
                }
                start = 0;
                end = 0;
            } else {
                //返回初始状态
                fn.back();
            }
        }
    }
}